/*
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * File: SMO_Arctan_FOC.c
 *
 * Code generated for Simulink model 'SMO_Arctan_FOC'.
 *
 * Model version                  : 2.7
 * Simulink Coder version         : 9.8 (R2022b) 13-May-2022
 * C/C++ source code generated on : Fri Oct 11 14:25:16 2024
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "SMO_Arctan_FOC.h"
#include "rtwtypes.h"
#include "Schedul_10msTask.h"
#include "Speed_Control.h"
#include "SMO_FOC.h"
#include "rt_nonfinite.h"

/* Exported block parameters */
real32_T SMO_Kslide = 0.65F;           /* Variable: SMO_Kslide
                                        * Referenced by:
                                        *   '<S13>/Constant'
                                        *   '<S14>/Constant'
                                        *   '<S15>/Constant'
                                        *   '<S16>/Constant'
                                        *   '<S17>/Constant'
                                        *   '<S18>/Constant'
                                        */
real32_T SMO_MaxSMCError = 8.2F;       /* Variable: SMO_MaxSMCError
                                        * Referenced by:
                                        *   '<S13>/Constant1'
                                        *   '<S16>/Constant1'
                                        */
real32_T SMO_OffsetAngle = 0.78F;      /* Variable: SMO_OffsetAngle
                                        * Referenced by: '<S23>/Constant1'
                                        */

/* Block signals (default storage) */
B_SMO_Arctan_FOC_T SMO_Arctan_FOC_B;

/* Block states (default storage) */
DW_SMO_Arctan_FOC_T SMO_Arctan_FOC_DW;

/* External inputs (root inport signals with default storage) */
ExtU_SMO_Arctan_FOC_T SMO_Arctan_FOC_U;

/* Real-time model */
static RT_MODEL_SMO_Arctan_FOC_T SMO_Arctan_FOC_M_;
RT_MODEL_SMO_Arctan_FOC_T *const SMO_Arctan_FOC_M = &SMO_Arctan_FOC_M_;
static void rate_scheduler(void);

/*
 *         This function updates active task flag for each subrate.
 *         The function is called at model base rate, hence the
 *         generated code self-manages all its subrates.
 */
static void rate_scheduler(void)
{
  /* Compute which subrates run during the next base time step.  Subrates
   * are an integer multiple of the base rate counter.  Therefore, the subtask
   * counter is reset when it reaches its limit (zero means run).
   */
  (SMO_Arctan_FOC_M->Timing.TaskCounters.TID[1])++;
  if ((SMO_Arctan_FOC_M->Timing.TaskCounters.TID[1]) > 199) {/* Sample time: [0.01s, 0.0s] */
    SMO_Arctan_FOC_M->Timing.TaskCounters.TID[1] = 0;
  }
}

/* Model step function */
void SMO_Arctan_FOC_step(real32_T arg_Target_Speed, real32_T arg_Iabc[3],
  real32_T arg_Vabc_PU[3])
{
  /* Copy value for root inport '<Root>/Target_Speed' since it is accessed globally */
  SMO_Arctan_FOC_U.target_speed = arg_Target_Speed;

  /* Copy value for root inport '<Root>/Iabc' since it is accessed globally */
  {
    int32_T i;
    for (i = 0; i < 3; i++) {
      SMO_Arctan_FOC_U.iabc[i] = arg_Iabc[i];
    }
  }

  /* Delay: '<S1>/Delay' */
  SMO_Arctan_FOC_B.Delay = SMO_Arctan_FOC_DW.Delay_DSTATE;
  if (SMO_Arctan_FOC_M->Timing.TaskCounters.TID[1] == 0) {
    /* S-Function (fcgen): '<S1>/Function-Call Generator5' incorporates:
     *  SubSystem: '<S1>/Schedul_10msTask'
     */
    SMO_Arctan_FOC_Schedul_10msTask();

    /* End of Outputs for S-Function (fcgen): '<S1>/Function-Call Generator5' */
  }

  /* S-Function (fcgen): '<S1>/Function-Call Generator3' incorporates:
   *  SubSystem: '<S1>/Speed_Control'
   */
  SMO_Arctan_FOC_Speed_Control();

  /* End of Outputs for S-Function (fcgen): '<S1>/Function-Call Generator3' */

  /* S-Function (fcgen): '<S1>/Function-Call Generator4' incorporates:
   *  SubSystem: '<S1>/SMO_FOC'
   */
  SMO_Arctan_FOC_SMO_FOC();

  /* End of Outputs for S-Function (fcgen): '<S1>/Function-Call Generator4' */

  /* Outport: '<Root>/Vabc_PU' */
  arg_Vabc_PU[0] = SMO_Arctan_FOC_B.Sum6[0];
  arg_Vabc_PU[1] = SMO_Arctan_FOC_B.Sum6[1];
  arg_Vabc_PU[2] = SMO_Arctan_FOC_B.Sum6[2];

  /* Update for Delay: '<S1>/Delay' incorporates:
   *  UnitDelay: '<S28>/Unit Delay'
   */
  SMO_Arctan_FOC_DW.Delay_DSTATE = SMO_Arctan_FOC_DW.UnitDelay_DSTATE_no;

  /* Update absolute time for base rate */
  /* The "clockTick0" counts the number of times the code of this task has
   * been executed. The resolution of this integer timer is 5.0E-5, which is the step size
   * of the task. Size of "clockTick0" ensures timer will not overflow during the
   * application lifespan selected.
   */
  SMO_Arctan_FOC_M->Timing.clockTick0++;
  if (SMO_Arctan_FOC_M->Timing.TaskCounters.TID[1] == 0) {
    /* Update absolute timer for sample time: [0.01s, 0.0s] */
    /* The "clockTick1" counts the number of times the code of this task has
     * been executed. The resolution of this integer timer is 0.01, which is the step size
     * of the task. Size of "clockTick1" ensures timer will not overflow during the
     * application lifespan selected.
     */
    SMO_Arctan_FOC_M->Timing.clockTick1++;
  }

  rate_scheduler();
}

/* Model initialize function */
void SMO_Arctan_FOC_initialize(void)
{
  /* Registration code */

  /* initialize non-finites */
  rt_InitInfAndNaN(sizeof(real_T));

  /* SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator3' incorporates:
   *  SubSystem: '<S1>/Speed_Control'
   */
  SMO_Arctan_F_Speed_Control_Init();

  /* End of SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator3' */

  /* SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator4' incorporates:
   *  SubSystem: '<S1>/SMO_FOC'
   */
  SMO_Arctan_FOC_SMO_FOC_Init();

  /* End of SystemInitialize for S-Function (fcgen): '<S1>/Function-Call Generator4' */

  /* Enable for S-Function (fcgen): '<S1>/Function-Call Generator5' incorporates:
   *  SubSystem: '<S1>/Schedul_10msTask'
   */
  SMO_Arc_Schedul_10msTask_Enable();

  /* End of Enable for S-Function (fcgen): '<S1>/Function-Call Generator5' */

  /* Enable for S-Function (fcgen): '<S1>/Function-Call Generator4' incorporates:
   *  SubSystem: '<S1>/SMO_FOC'
   */
  SMO_Arctan_FOC_SMO_FOC_Enable();

  /* End of Enable for S-Function (fcgen): '<S1>/Function-Call Generator4' */
}

/* Model terminate function */
void SMO_Arctan_FOC_terminate(void)
{
  /* (no terminate code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
